from flask_shop.utils.response_message import *
from flask_shop.apis.user_module.user_models import *
from flask_restful import Resource, reqparse
from ..classes import FArgument
from .switch_models import Switch


class ChangeSwitch(Resource):
    # 更新用户信息
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument(FArgument(name='project_type', type=int))
        parser.add_argument(FArgument(name='is_on', type=bool))

        try:
            args = parser.parse_args()
            project_type = args.get('project_type')
            is_on = args.get('is_on')

            switch_ins: Switch = Switch.query.filter_by(project_type=project_type).first()
            if switch_ins:
                switch_ins.is_on = is_on
                db.session.commit()
                return get_response(ResponseType.success)
            else:
                return get_response(ResponseType.field_error, msg="未查询到此项目的开关")
        except Exception as e:
            rsp = get_error_response(e)
            return rsp


class SwitchStatus(Resource):
    # 更新用户信息
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument(FArgument(name='project_type', type=int))

        try:
            args = parser.parse_args()
            project_type = args.get('project_type')

            switch_ins: Switch = Switch.query.filter_by(project_type=project_type).first()
            if switch_ins:

                return get_response(ResponseType.success, data=switch_ins.dict())
            else:
                return get_response(ResponseType.field_error, msg="未查询到此项目的开关")
        except Exception as e:
            rsp = get_error_response(e)
            return rsp
